<?php
/**
 * 会员管理
 * PHP version 5
 * @package   Antonia
 * @author    Antonia <471699715@qq.com>
 * @copyright 2016 Antonia.
 */
namespace Admin\Controller;
use Think;
class UserController extends AdmincommonController
{
    /**
     * 显示用户列表
     * @param HttpRequest $request
     * @return html
     * @author Antonia
     */
    public function index () {
        $filters = array();
        !empty($_POST['userName']) && $filters['userName'] = array('like','%' . remove_xss($_POST['userName']) . '%');
        !empty($_POST['userMobile']) && $filters['userMobile'] = array('like','%' . remove_xss($_POST['userMobile']) . '%');
        $user = D("Users");
        $_GET['p'] = $_POST['p'];
        $count = $user->where($filters)->count();
        if (!empty($_POST['numPerPage'])){
            $numPerPage = $_POST['numPerPage'];
        } else {
            $numPerPage = 20;
        }
        $Page  =  new \Think\Page($count,$numPerPage);
        $list = $user->limit($Page->firstRow.','.$Page->listRows)
            		  ->where($filters)
            		  ->select();
        $this->assign("list",$list);
        $this->assign('filters', $_POST);
        $this->assign ('totalCount', $count);
        $this->assign ('numPerPage', $Page->listRows);
        $this->assign ('currentPage', !empty($_GET['p'])?$_GET['p']:1);
        $this->display();
    }

    /**
     * 禁用用户
     * @param HttpRequest $request
     * @return void
     * @author Antonia
    */
    public function delete() {
        $re = array();
        if (!empty($_GET['id'])) {
            $users = D("Users");
            $result = $users->find(remove_xss($_GET['id']));
            if (false !== $result) {
                $data["users_id"] = $result["users_id"];
                $data["status"] = 1;
                $rs = $users->save($data);
                if (!empty($rs)) {
                    $re['statusCode'] = "200";
                    $re['message'] = "禁用成功!";                    
                }else {
                $re['statusCode'] = "300";
                $re['message'] = "禁用失败，请重试!";
                }
            } else {
                $re['statusCode'] = "300";
                $re['message'] = "该记录不存在！!";
            }
        } else {
                $re['statusCode'] = "300";
                $re['message'] = "非法提交";
        }
        echo json_encode($re);
    }

    /**
     * 恢复用户
     * @param HttpRequest $request
     * @return void
     * @author Antonia
    */
    public function insert() {
        $re = array();
        if (!empty($_GET['id'])) {
            $users = D("Users");
            $result = $users->find(remove_xss($_GET['id']));
            if (false !== $result) {
                $data["users_id"] = $result["users_id"];
                $data["status"] = 0;
                $rs = $users->save($data);
                if (!empty($rs)) {
                    $re['statusCode'] = "200";
                    $re['message'] = "恢复成功!";                    
                }else {
                $re['statusCode'] = "300";
                $re['message'] = "恢复失败，请重试!";
                }
            } else {
                $re['statusCode'] = "300";
                $re['message'] = "该记录不存在！!";
            }
        } else {
                $re['statusCode'] = "300";
                $re['message'] = "非法提交";
        }
        echo json_encode($re);
    }

    /**
     * 禁用选定用户
     * @param HttpRequest $request
     * @return void
     * @author Antonia
    */
    public function delAll() {
        $Users = D("Users");
        $re = array();
        $map['users_id']  = array('in', explode(',', $_POST["ids"]));
        $data["status"] = 1;
        $result  = $Users->where($map)->save($data);
        if($result) {
            $re['statusCode'] = "200";
            $re['message'] = "禁用成功!";
        } else {
            $re['statusCode'] = "300";
            $re['message'] = "禁用失败，请重试!";
        }
        echo json_encode($re);
    }

    /**
     * 恢复选定用户
     * @param HttpRequest $request
     * @return void
     * @author Antonia
    */
    public function insertALL() {
        $Users = D("Users");
        $re = array();
        $map['users_id']  = array('in', explode(',', $_POST["ids"]));
        $data["status"] = 0;
        $result  = $Users->where($map)->save($data);
        if($result) {
            $re['statusCode'] = "200";
            $re['message'] = "恢复成功!";
        } else {
            $re['statusCode'] = "300";
            $re['message'] = "恢复失败，请重试!";
        }
        echo json_encode($re);
    }
}